const {request} = require('../../utils/request');
/* 这个自定义组件js结构 */
Component({
  options: {
    multipleSlots: true // 在组件定义时的选项中启用多slot支持
  },
  properties: {
    // 这里定义了innerText属性，属性值可以在组件使用时指定
    innerText: {
      type: String,
      value: 'default value',
    }
  },
  data: {
    showCheckPhone:false,
    userInfo:[],
    toDo:null
  },
  methods: {
    isHavePhone(toDo){ 
      console.log(wx.getStorageSync('userInfo'))
      let userInfo=wx.getStorageSync('userInfo');
      var that =this;//判断用户是否授权,且是否有手机号（注册）
      this.setData({
         userInfo:wx.getStorageSync('userInfo'),
         toDo: toDo 
      })
      console.log('phonelength'+(that.data.userInfo.phone.length))
      if(that.data.userInfo.phone!=null){
        if(that.data.userInfo.phone.length==0){//用户未授权手机号
          console.log('noPhone')
           that.setData({
            showCheckPhone:true
           })
         }
         that.data.toDo();
      }else{
        that.setData({
          showCheckPhone:true
         })
        
      }
    
    }, 

    getPhoneNumber(e) {//用户按了允许授权电话号码按钮
      let that = this;
      console.log(e.detail.iv)
      if(e.detail.iv){ //用户点击授权手机号
            let params={
              encryptedData:e.detail.encryptedData,
              iv:e.detail.iv,
            };
  
            request('/wechat/miniprogram/user/phoneNum','POST',params).then(function(res){
            wx.setStorageSync('userInfo', res.data.data);
            that.setData({
                showCheckPhone:false
              })
            that.data.toDo();
            })
      }else{//用户点击拒绝授权手机号
        wx.showModal({
          title: '警告',
          content: '您点击了拒绝获取手机号，将无法进行之后操作!',
          showCancel: false,
          confirmText: '返回授权',
          success: function (res) {
              if (res.confirm) {
                  console.log('用户点击了“返回授权”')
              }
          }
      })
      }
   }
 }
})


